<template>
  <el-dialog
      v-model="typeEditWinDialog"
      title="编辑类型"
      width="50%">
    <template #default>
      <el-form :model="editType" label-width="auto">
        <el-form-item label="设备类型编码">
          <el-input v-model="editType.machineTypeCode"/>
        </el-form-item>
        <el-form-item label="设备类型名称">
          <el-input v-model="editType.machineTypeName"/>
        </el-form-item>
        <el-form-item label="是否启用">
          <el-select style="width: 100px" v-model="editType.machineTypeStatus">
            <el-option :value="1" label="是"/>
            <el-option :value="2" label="否"/>
          </el-select>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="editType.machineTypeNotes"/>
        </el-form-item>
      </el-form>
    </template>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="cancelEditTask">取消</el-button>
        <el-button type="primary" @click="editTaskSubmit">
          提交
        </el-button>
      </div>
    </template>
  </el-dialog>

</template>
<script setup>

import {onBeforeUnmount, ref, shallowRef, watch} from "vue";
import typeRequest from "@/api/type.js"
import {ElMessage} from "element-plus";
const props = defineProps(['machineTypeId']);
const emits = defineEmits(['editComplate'])
let typeEditWinDialog = ref(false);
let editType = ref({});
function cancelEditTask(){
  editType = ref({});
  typeEditWinDialog.value = false;
}
function editTaskSubmit(){
  console.log(editType.value);
  typeRequest.updateType(editType.value,res=>{
    if(res.code == 200){
      ElMessage({
        type:"success",
        message:"编辑成功"
      });
      // 关闭弹窗
      cancelEditTask();
      // 触发编辑完成的事件
      emits('editComplate');
    }else{
      ElMessage({
        type:"warning",
        message:"任务编辑失败"
      });
    }
  })
}

function loadTypeById(){
  typeRequest.selectByTypeId(props.machineTypeId,res=>{
    if(res.data){
      // 确实加载到要编辑的数据了
      editType.value = res.data;
      typeEditWinDialog.value = true;
    }
  });
}

watch(()=>props.machineTypeId,()=>{
  if(props.machineTypeId){
    loadTypeById();
  }
});
</script>

<style scoped>

</style>